import XLSX from 'xlsx';

export const readExcel = function (blob_file) {
  const reader = new FileReader();
  return new Promise((resolve, reject) => {
    reader.onload = () => {
      const buffer = reader.result;
      const bytes = new Uint8Array(buffer);
      const length = bytes.byteLength;
      let binary = '';
      for (let i = 0; i < length; i++) {
        binary += String.fromCharCode(bytes[i]);
      }
      const wb = XLSX.read(binary, {
        type: 'binary'
      });
      const sheetData = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
      resolve(sheetData);
    };
    reader.onerror = (err) => {
      reject(err);
    };
    reader.readAsArrayBuffer(blob_file);
  });
};
